package com.walala.dispatcher.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.walala.dispatcher.service.DispatcherService;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
	
	private static Logger logger = LoggerFactory.getLogger(UdpServerHandler.class);

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {

		ByteBuf byteBuf = packet.content();
		int length = byteBuf.readableBytes();
        byte[] msgBytes = new byte[length];
        byteBuf.readBytes(msgBytes);
        String msg = new String(msgBytes, CharsetUtil.UTF_8);
        logger.info("调度服务收到消息,消息内容:{}", msg);
        try {
            DispatcherService.process(ctx, packet, msg);
        } catch (Exception e) {
            logger.error("服务器内部错误,客户端消息内容:{}", msg);
        }
		
	}


}
